package com.iweb;


import org.apache.commons.dbcp2.BasicDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * 使用 spring 注入的方式来初始化连接池
 * xml和配置类的方式
 */
// 一般方法
public class TestDataSource {
    public static void main(String[] args) throws Exception{

        // 创建数据源对象 : 一定是 dataSource 的子类
        BasicDataSource dataSource = new BasicDataSource();

        // 提供基本参数
        dataSource.setUrl("jdbc:mysql://192.168.100.10:3306/db_info?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull");
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");

        // 设定连接池的初始化大小
        dataSource.setInitialSize(10);

        Connection connection = dataSource.getConnection();

        PreparedStatement preparedStatement = connection.prepareStatement("select * from tb_user");

        ResultSet resultSet = preparedStatement.executeQuery();

        while (resultSet.next()){
            System.out.println(resultSet.getObject(1)+"\t"+resultSet.getObject(2));
        }

    }
}
